package com.sunmouse.toolkit.fragment.web;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.widget.Button;

import com.sunmouse.toollib.utils.JumpUtils;
import com.sunmouse.toollib.utils.LogUtils;
import com.sunmouse.toollib.utils.ToastUtils;
import com.sunmouse.toollib.widget.SimpleWebView;
import com.sunmouse.toolkit.activity.MainActivity;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.fragment.BaseFragment;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class JsFragment extends BaseFragment {


    @BindView(R.id.m_web)
    SimpleWebView mWeb;
    @BindView(R.id.update_btn)
    Button updateBtn;
    Unbinder unbinder;

    public JsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_js, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {
//        ClassLoader
//        Array.set();
        Class cls;
        //this.mWeb.getSettings().setJavaScriptEnabled(true);
        String site = "file:///android_asset/html/android_js.html";
//        String site = "file:///android_asset/setIp.html";
//        String site = "file:///" + FileUtils.getStoragePath() + "/resola/setIp.html";
//        String url="http://192.168.1.80:8080/";
        File file = new File(",", "");
        this.mWeb.loadUrl(site);
        this.updateBtn.setOnClickListener(this);

        this.mWeb.addJavascriptInterface(new JavaMethod(), "javaMethod");
    }

    /**
     * provide methods for js
     */
    class JavaMethod {
        @JavascriptInterface
        public void jump() {
            JumpUtils.simpleJump(getActivity(), MainActivity.class);
        }

        @JavascriptInterface
        public void search() {
            ToastUtils.simpleToast(getActivity(), "invoke by js");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.update_btn:
//                this.mWeb.loadUrl("javascript:" + method + "('" + ip + "','" + port + "')");
                LogUtils.e("-----------js-------------");
//                String color = "#FF4081";
//                String height = "25";
//                String site="https://www.baidu.com/";
//                this.mWeb.loadUrl("javascript:greed('"+site+"','"+height+"')");
                String ip = "192.168.1.1";
                this.mWeb.loadUrl("javascript:getPram('" + ip + "')");
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
